// UserDefinedDisplayContentQueryHandler.cpp: implementation of the UserDefinedDisplayContentQueryHandler class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "kwservercore.h"
#include "UserDefinedDisplayContentQueryHandler.h"
#include "DatabaseManager.h"
#include "UserDefinedDisplaySet.h"

#include "CommonLib/Structure.h"
#include "CommonLib/Packet.h"

#include "CommServerLib/CommWithClient.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

UserDefinedDisplayContentQueryHandler::UserDefinedDisplayContentQueryHandler(const Packet& packet, HANDLE hClient):
	BaseRequestHandler(packet, hClient)
{
}

UserDefinedDisplayContentQueryHandler::~UserDefinedDisplayContentQueryHandler()
{
}

int UserDefinedDisplayContentQueryHandler::Handling()
{
	UserDefinedDisplayQueryStruct * pUserDefinedDisplayQueryStruct = (UserDefinedDisplayQueryStruct * )m_pPacket->GetpData();
	UserDefinedDisplayAckStruct * pUserDefinedDisplayAckStruct = QueryUserDefinedDisplayContent(pUserDefinedDisplayQueryStruct->m_dwMethodType, pUserDefinedDisplayQueryStruct->m_szUserId, pUserDefinedDisplayQueryStruct->m_szDisplayName);

	Packet packetAck(pUserDefinedDisplayAckStruct->m_dwBufSize, (BYTE *)pUserDefinedDisplayAckStruct);
	packetAck.m_dwId = m_pPacket->m_dwId;
	CommWithClient::Instance()->SendTo(packetAck, m_hClient);

	delete pUserDefinedDisplayAckStruct;

	return 0;
}

UserDefinedDisplayAckStruct * UserDefinedDisplayContentQueryHandler::QueryUserDefinedDisplayContent(DWORD dwMethodType, const CString& strUserId, const CString& strDisplayName)
{
	CDatabase * pDatabase = DatabaseManager::Instance()->GetpDatabase();
	CUserDefinedDisplaySet rsUserDefinedDisplaySet(pDatabase);
	rsUserDefinedDisplaySet.m_strFilter.Format("UserId = '%s' AND DisplayName = '%s'", strUserId, strDisplayName);
	rsUserDefinedDisplaySet.Open(AFX_DB_USE_DEFAULT_TYPE, NULL, CRecordset::readOnly);
	UserDefinedDisplayAckStruct * pUserDefinedDisplayAckStruct;
	if (rsUserDefinedDisplaySet.IsEOF())
	{
		pUserDefinedDisplayAckStruct = new UserDefinedDisplayAckStruct;
		pUserDefinedDisplayAckStruct->m_nMethodAck = VCS_MR_TEMPLETNOTEXIST;
		pUserDefinedDisplayAckStruct->m_dwBufSize = sizeof(UserDefinedDisplayAckStruct);
		pUserDefinedDisplayAckStruct->m_dwNumOfItems = 0;
	}
	else
	{
		int nDataSize = rsUserDefinedDisplaySet.m_Content.GetLength();
		int nBufSize = sizeof(UserDefinedDisplayAckStruct)+nDataSize;
		pUserDefinedDisplayAckStruct = (UserDefinedDisplayAckStruct *) new BYTE[nBufSize];
		pUserDefinedDisplayAckStruct->m_nMethodAck = VCS_MR_OK;
		pUserDefinedDisplayAckStruct->m_dwBufSize = nBufSize;
		pUserDefinedDisplayAckStruct->m_dwNumOfItems = 1;
		strcpy(pUserDefinedDisplayAckStruct->m_UserDefinedDisplayItems[0].m_szUserId, rsUserDefinedDisplaySet.m_UserId);
		strcpy(pUserDefinedDisplayAckStruct->m_UserDefinedDisplayItems[0].m_szDisplayName, rsUserDefinedDisplaySet.m_DisplayName);
		pUserDefinedDisplayAckStruct->m_UserDefinedDisplayItems[0].m_nShareFlag = rsUserDefinedDisplaySet.m_ShareFlag;
		pUserDefinedDisplayAckStruct->m_UserDefinedDisplayItems[0].m_dwContentSize = nDataSize;
		strcpy(pUserDefinedDisplayAckStruct->m_UserDefinedDisplayItems[0].m_szContent, (LPCSTR)rsUserDefinedDisplaySet.m_Content);
	}
	pUserDefinedDisplayAckStruct->m_dwMethodType = dwMethodType;
	rsUserDefinedDisplaySet.Close();

	return pUserDefinedDisplayAckStruct;
}